package com.kethink;

import java.util.Stack;

/**
 * @Description :
 * @Author: demo
 * @Date: 2018/12/27 0:36
 * Copyright (C), 2017-2018, 沃顿信息科技有限公司
 */
public class LeeCode_844 {

    public static boolean backspaceCompare(String S, String T) {

        Stack s = new Stack();
        Stack t = new Stack();
        for(char c : S.toCharArray()){
            if(c!='#'){
                s.push(c);
            }else {
                if(!s.isEmpty()){
                    s.pop();
                }
            }
        }
        for(char c : T.toCharArray()){
            if(c!='#'){
                t.push(c);
            }else {
                if(!t.isEmpty()){
                    t.pop();
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        StringBuilder tb = new StringBuilder();

        while (!s.isEmpty()){
            sb.append(s.pop());
        }

        while (!t.isEmpty()){
            tb.append(t.pop());
        }

        return sb.toString().equals(tb.toString());
    }

    public static void main(String[] args){
        String S = "ab#c";
        String T = "ad#c";
        System.out.println(backspaceCompare(S,T));
    }
}
